package history.hot100;

import java.util.HashMap;

// 560. 和为K的子数组:https://leetcode-cn.com/problems/subarray-sum-equals-k/
// https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/he-wei-kde-zi-shu-zu-by-leetcode-solution/
public class LeetCode_560 {
    public int subarraySum(int[] nums, int k) {
        int count =0, pre = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0,1);
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            if (map.containsKey(pre - k)) {
                count += map.get(pre-k);
            }
            map.put(pre, map.getOrDefault(pre, 0)+1);
        }
        return count;
    }
}
